package net.hiwii.system.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import net.hiwii.obj.time.DateTime;
import net.hiwii.obj.time.YearTime;

public class TimeUtil {
	public static final SimpleDateFormat yearfmt = new SimpleDateFormat("yyyy");
	public static final SimpleDateFormat monthfmt = new SimpleDateFormat("yyyyMM");
	public static final SimpleDateFormat datefmt = new SimpleDateFormat("yyyyMMdd");
	public static final SimpleDateFormat hourfmt = new SimpleDateFormat("yyyyMMdd HH");
	public static final SimpleDateFormat minitefmt = new SimpleDateFormat("yyyyMMdd HH:mm");
	public static final SimpleDateFormat secondfmt = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
	
	public static YearTime yearTime(){
		Calendar cal = Calendar.getInstance();
		YearTime time = new YearTime();
		time.setYear(String.valueOf(cal.get(Calendar.YEAR)));
		return time;
	}
	public static YearTime yearTime(String year){
		if(isYearTime(year) != null) {
			YearTime time = new YearTime();
			time.setYear(year);
			return time;
		}
		return null;
	}
	
	public static DateTime dateTime(){
		DateTime time = new DateTime();
		Calendar cal=Calendar.getInstance();  
		time.init(cal);
		return time;
	}
	public static DateTime dateTime(String str){
		Date date = isDateTime(str);
		if(date != null) {
			DateTime time = new DateTime();
			Calendar cal = Calendar.getInstance();  
		    cal.setTime(date); 
			time.init(cal);
			return time;
		}
		return null;
	}
	
	public static Date isYearTime(String time) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			Date date = sdf.parse(time);
			if(date != null) {
				return date;
			}
			return null;
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static Date isMonthTime(String time) {
		try {
			Date date = monthfmt.parse(time);
			if(date != null) {
				return date;
			}
			return null;
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static Date isDateTime(String time) {
		String time1 = time.replaceAll("[./-]", "");
		try {
			Date date = datefmt.parse(time1);
			if(date != null) {
				return date;
			}
			return null;
		} catch (ParseException e) {
			return null;
		}
	}
}
